﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace XemlUtilities.ISA.Protocol
{
    class MetaboliteExtraction : ProtocolBase
    {
        #region constructors
        public MetaboliteExtraction() : base()
        {
            Name = "extraction";
            Type = "metabolite extraction";
            Description = "The protocoll of extracting metabolites from the sampled (plant) material.";
            this.Register();
        }
        #endregion constructors

        public static Dictionary<String, String> mappings = new Dictionary<string, string>{
             {"metabolic inactivation", "metabolic inactivation"},
             {"extraction", "extraction solvent"},
             {"derivatization", "derivatization"}
        };

        public static void InitProtocols(Guid xemlId, string connectionString) {
            using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
            using (cmd.Connection = new System.Data.SqlClient.SqlConnection(connectionString))
            {
                cmd.CommandText = @"
                    SELECT distinct GC_Method_details.FK_Method_repository AS attribute, GC_Method_details.text, GC_Method_details.details
					FROM GC_Chromatogram 
					INNER JOIN GC_Method_details ON GC_Chromatogram.FK_Method = GC_Method_details.FK_Method
					INNER JOIN xeml.Document AS [document]
					INNER JOIN xeml.SampleMapping AS s on document.Id = s.FK_Document on GC_Chromatogram.name = s.Value
					WHERE document.XemlId = @xemlId
					and s.SubKey = 'Chromatogram';
    			";
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Parameters.AddWithValue("@xemlId", xemlId);

                try
                {
                    cmd.Connection.Open();
                    cmd.Transaction = cmd.Connection.BeginTransaction(IsolationLevel.ReadUncommitted);
                    ProtocolBase curProtocol = null;
                    using (SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection | CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
                    {
                        while (reader.Read())
                        {
                            String curProtocolName = reader.GetString(0);
                            if (MetaboliteExtraction.mappings.ContainsKey(curProtocolName)) // only add the mapped keys
                            {
                                curProtocolName = MetaboliteExtraction.mappings[curProtocolName];

                                if (curProtocol == null || curProtocol.Name != curProtocolName)
                                {
                                    curProtocol = new MetaboliteExtraction();
                                    curProtocol.Name = curProtocolName;
                                }

                                Parameter curParameter = new Parameter();
                                curParameter.Name = reader.GetString(1);
                                curParameter.Value = reader.GetString(2);
                                curProtocol.AddParameter(curParameter);
                            }
                        }
                    }
                }
                finally
                {
                    cmd.Connection.Close();
                }
            }
        }
    }
}
